package org.cloudie.docs.editor.client.windows;

import org.cloudie.docs.editor.client.behaviors.HasExploreBehavior;
import org.cloudie.docs.editor.client.behaviors.HasReceiveBehavior;
import org.cloudie.docs.editor.client.widgets.ClonePanel;
import org.cloudie.docs.editor.client.widgets.ExplorerIcon;
import org.cloudie.docs.editor.client.widgets.Window;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gen2.table.client.FixedWidthFlexTable;
import com.google.gwt.gen2.table.client.FixedWidthGrid;
import com.google.gwt.gen2.table.client.ScrollTable;
import com.google.gwt.gen2.table.client.AbstractScrollTable.SortPolicy;
import com.google.gwt.gen2.table.override.client.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public abstract class ExplorerWindow<T extends HasExploreBehavior> extends Window {

  protected boolean useProxyBehavior = true;
  protected ScrollTable scrollTable;
  protected FixedWidthFlexTable headerTable;
  protected FixedWidthGrid dataTable;
  protected T currentFolder, previousFolder;
  
  protected ExplorerWindow(String title, String[] columns, boolean useProxyBehavior) {
    super(title);
    hide();
    this.useProxyBehavior = useProxyBehavior;
    VerticalPanel vp = new VerticalPanel();
    vp.setStylePrimaryName("cloudie-explorer");
    
    dataTable = new FixedWidthGrid();
    dataTable.setCellSpacing(0);
    dataTable.setWidth("100%");
    dataTable.resize(0, columns.length);
    
    headerTable = new FixedWidthFlexTable();
    headerTable.setCellSpacing(0);
    headerTable.setWidth("100%");
    headerTable.insertRow(0);
    FlexCellFormatter formatter = headerTable.getFlexCellFormatter();
    for (int i=0; i<columns.length; i++) {
      headerTable.insertCell(0, i);
	  headerTable.setHTML(0, i, "<b>" + columns[i] + "</b>");
	  formatter.setHorizontalAlignment(0, i, HasHorizontalAlignment.ALIGN_LEFT);
    }
    
    scrollTable = new ScrollTable(dataTable, headerTable);
    scrollTable.setStylePrimaryName("cloudie-explorer-table");
    scrollTable.setSize("600px", "400px");
    scrollTable.setSortPolicy(SortPolicy.SINGLE_CELL);
    scrollTable.setResizePolicy(ScrollTable.ResizePolicy.UNCONSTRAINED);
    vp.add(scrollTable);
    
    setContentWidget(vp);
    setContentSize(600, 400);
    closeButton.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        event.stopPropagation();
        event.preventDefault();
        hide();
      }
    });
    minimizeButton.setVisible(false);
    buttons.setWidth("18px");
  }
  
  public void clearEntries() {
    dataTable.resizeRows(0);
  }
  
  public abstract void refresh();
  public void show(T folder, T[] entries) {
	setLoading(false);
    clearEntries();
    previousFolder = currentFolder;
    currentFolder = folder;
    if (previousFolder == null) {
      previousFolder = currentFolder;
    }
	titleLabel.setText(folder.getExplorerEntryLabel());
  }

  public void setLoading(boolean loading) {
	if (loading) {
	  clearEntries();
	  scrollTable.setVisible(false);
      contentWidget.setStylePrimaryName("cloudie-Loading");
      show();
	} else {
	  scrollTable.setVisible(true);
	  contentWidget.setStylePrimaryName("cloudie-explorer");
	}
  }
  
  @Override
  public void onResize(int width, int height) {
    super.onResize(width, height);
    scrollTable.setPixelSize(width, height);
  }
  
  protected void addEntry(ExplorerIcon icon, String[] columns) {
    int r = dataTable.getRowCount();
    dataTable.insertRow(r);
    for (int i=0; i<columns.length; i++) {
      dataTable.setText(r, i, columns[i]);
    }
    Widget w = icon;
    if (registeredDragController != null) {
      if (useProxyBehavior) {
        w = new ClonePanel(registeredDragController, icon);
      } else {
        registeredDragController.makeDraggable(icon);
        SimplePanel tmp = new SimplePanel();
        tmp.setWidget(icon);
        w = tmp;
      }
    }
    dataTable.setWidget(r, 0, w);
    if (icon instanceof HasReceiveBehavior) {
	  ((HasReceiveBehavior) icon).setDragController(registeredDragController);
    }
  }
  
  public T getCurrentFolder() {
    return currentFolder;
  }
  
  public T getPreviousFolder() {
    return previousFolder;
  }
  
}
